package com.lc.cehui.ui.activity.home;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.dialog.ComplaintDialog;
import com.lc.cehui.http.request.ReporttionApi;
import com.lc.cehui.http.request.UpdateImageApi;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.http.response.UpdateImageBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.ImageNineAdapter;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class ComplaintDetailsActivity extends AppActivity {
    @BindView(R.id.tv_yuanyin)
    TextView tvYuanyin;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private String msg_id;
    private ImageNineAdapter adapter;
    private String picurl;
    private RxPermissions rxPermissions;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_complaintdetails;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        rxPermissions = new RxPermissions(this);
        tvYuanyin.setText("举报原因       "+getIntent().getStringExtra("title"));
        msg_id = getIntent().getStringExtra("msgId");
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
      adapter = new ImageNineAdapter(this, new ArrayList<Object>(), true);
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);
        adapter.setOnPhotoClickListener(new ImageNineAdapter.onPhotoClickListener() {
            @Override
            public void onClick(View view, int position) {
                startImageBrowse(adapter.getList(), position);
            }
        });
        adapter.setonDeleteClickListener(new ImageNineAdapter.onDeleteClickListener() {
            @Override
            public void onClick(View view, int position) {
                adapter.getList().remove(position);
                //添加脚布局
                if (adapter.getmFooterView() == null)
                    setFooterView(recyclerView, adapter);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });

        setFooterView(recyclerView, adapter);
    }

    private void setFooterView(RecyclerView view, ImageNineAdapter adapter) {
        View footer = LayoutInflater.from(this).inflate(R.layout.adapter_image_footer, view, false);
        ImageView add = footer.findViewById(R.id.iv_add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rxPermissions
                        .request(Manifest.permission.CAMERA)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {

                                        showAlbumlot(PictureConfig.CHOOSE_REQUEST, 3);

                                } else {
                                    ToastUtils.show("请求权限失败,相机不可用");
                                }
                            }
                        });
            }
        });
        adapter.setmFooterView(footer);
    }

    @OnClick({ R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.bt_submit:
                reporttion();

                break;
        }
    }

    private void reporttion() {
        EasyHttp.post(this)
                .api(new ReporttionApi().setUserid(SPUtil.get(this, IntentKey.USERID,0)).setMsg_id(msg_id).setContent(etContent.getText().toString()).setImg(picurl))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            ComplaintDialog dialog=new ComplaintDialog(ComplaintDetailsActivity.this,ComplaintDetailsActivity.this);
                            dialog.show();

                        }

                    }
                });
    }

    private void getCommon_uploads(File list) {
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImg(list))
                .request(new OnHttpListener<UpdateImageBean>() {

                    @Override
                    public void onSucceed(UpdateImageBean data) {
//
                        if (data.getCode().equals("200")) {

                            adapter.getList().add(new File(data.getData().getImg()));
                            picurl = CourseUtils.listToString1(adapter.getList());

                                    if (adapter.getList().size() >= 3) {
                                        adapter.removeFooterView();//移除脚布局
                                    }


                            }

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {

                                       adapter.notifyDataSetChanged();

                                }
                            });
                        }



                    @Override
                    public void onFail(Exception e) {

                    }
                });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> result = PictureSelector.obtainMultipleResult(data);


                        adapter.getList().clear();
                        for (int i = 0; i < result.size(); i++) {

                            getCommon_uploads(new File(result.get(i).getRealPath()));
                        }



                    break;
            }
        }
    }
}
